home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 April / macformat-023.iso / Shareware City / Developers / ArrowCDEF / ArrowCDEF Tester.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-10  |  10.9 KB  |  336 lines  |  [TEXT/KAHL]

  1. /*******************************************************************************
  2.     FILE:            ArrowCDEF Tester.c
  3.     CREATED:        March 17, 1994
  4.     AUTHOR:            David Hay
  5.     DESCRIPTION:    A simple program to test the ArrowCDEF.
  6.     
  7.     VERSION:        1.1
  8.     
  9.     Copyright © 1994 by David Hay
  10. *******************************************************************************/
  11.  
  12. #include <ctype.h>
  13. #include "ArrowCDEF.h"
  14. #include "ArrowCDEFUtils.h"
  15.  
  16. #define kBaseResID        128
  17. #define kMoveToFront    (WindowPtr)-1L
  18.  
  19. #define kBWArrow        128
  20. #define kColorArrow        129
  21.  
  22. #define    kEnterKey        0x03
  23. #define    kReturnKey        0x0d
  24. #define kEscapeKey        0x1b
  25.  
  26. #define    kVisualDelay    8
  27. #define    kScrollDelay    15
  28.  
  29. #define    kArrowCntl        2
  30. #define    kOKOutline        3
  31. #define    kTextItem        4
  32. #define    kActiveItem        6
  33. #define kUseColorArrow    7
  34.  
  35. #define kChecked        1
  36. #define kUnchecked        0
  37.  
  38.  
  39. /*******************************************************************************
  40.  * FUNCTION:    ToolBoxInit
  41.  *
  42.  * INPUT:        None.
  43.  * RETURNS:        Nothing.
  44.  *
  45.  * DESCRIPTION:    Initializes the Macintosh toolbox managers
  46.  *******************************************************************************/
  47. static void ToolBoxInit( void )
  48. {
  49.     InitGraf(&thePort);
  50.     InitFonts();
  51.     FlushEvents(everyEvent, 0);
  52.     InitWindows();
  53.     InitMenus();
  54.     TEInit();
  55.     InitDialogs(nil);
  56.     MaxApplZone();
  57.     InitCursor();
  58. }
  59.  
  60. /*******************************************************************************
  61.  * FUNCTION:    DialogFilter
  62.  *
  63.  * INPUT:        DialogPtr theDialog        -- The dialog to filter the events for
  64.  *                EventRecord* theEvent    -- the event to filter.
  65.  *                short* itemHit            -- The item hit returned here
  66.  * RETURNS:        Boolean                    -- Did the filter handle the event?
  67.  *
  68.  * DESCRIPTION:    Handles routines intended for the given dialog. It intercepts
  69.  *                non-number entries into the text edit field and responds to
  70.  *                the Enter and Return keys.
  71.  *******************************************************************************/
  72. static pascal Boolean DialogFilter( DialogPtr theDialog,
  73.                              EventRecord* theEvent,
  74.                              short* itemHit )
  75. {
  76.     short    itemType;    /* The kind of dialog item returned from GetDItem */
  77.     Handle    itemHandle;    /* Handle to a dialog item */
  78.     Rect    itemRect;    /* Rectangle of a dialog item */
  79.     char    key;        /* the key that was pressed */
  80.     long    finalTicks;    /* number of ticks passed after a delay */
  81.     
  82.     switch( theEvent->what )
  83.     {
  84.         case keyDown:
  85.         case autoKey:
  86.             key = ( char ) ( ( theEvent->message ) & charCodeMask );
  87.             
  88.             if ( key == kReturnKey || key == kEnterKey )
  89.             {
  90.                     /*    Respond as if the OK button was pressed by hiliting the
  91.                     **    button for a visible amount of time
  92.                     **/
  93.                 GetDItem( theDialog, ok, &itemType, &itemHandle, &itemRect );
  94.                 HiliteControl( (ControlHandle)itemHandle, inButton );
  95.                 Delay( kVisualDelay, &finalTicks );
  96.                 HiliteControl( (ControlHandle)itemHandle, 0 );
  97.                 *itemHit = ok;
  98.                 return( true );
  99.             }
  100.             else
  101.             {
  102.                     /* Only allow numbers to be entered and beep at
  103.                     ** the user if they enter something besides numbers
  104.                     **/
  105.                 if ( !isdigit( key ) && !iscntrl( key ) && key != '-')
  106.                 {
  107.                     SysBeep( 10 );
  108.                     *itemHit = kTextItem;
  109.                     return( true );
  110.                 }
  111.             }
  112.             break;
  113.     }
  114.     return( false );    /* Getting here means we didn't handle the event */
  115. }
  116.  
  117. /*******************************************************************************
  118.  * FUNCTION:    ArrowActionProc
  119.  *
  120.  * INPUT:        ControlHandle theControl    -- The control affected
  121.  *                short part                    -- Part where the mouse is now
  122.  * RETURNS:        Nothing.
  123.  *
  124.  * DESCRIPTION:    Action procedure for the arrow control. It simply updates the
  125.  *                value of the text edit field by incrementing or decrementing the
  126.  *                value depending upon which part of the arrow is being pressed.
  127.  *                The longer the user presses a part of the arrow, the faster the
  128.  *                values increment or decrement.
  129.  *******************************************************************************/
  130. static pascal void ArrowActionProc( ControlHandle theControl, short part )
  131. {
  132.     long            theValue;    /* The current value of the control */
  133.     Str255            theString;    /* the string in the edit text */
  134.     DialogPtr        theDialog;    /* the dialog the control belongs to */
  135.     Rect            iRect;        /* Rectangle of a dialog item */
  136.     Handle            iHandle;    /* Handle to a dialog item */
  137.     short            iType;        /* The type of a dialog item */
  138.     static long        delay = kScrollDelay;    /* delay before an increment occurs */
  139.     static long        endTicks = 0;            /* tick count when we will increment */
  140.     static short    oldDir = kPlainArrow;    /* direction of the last time */
  141.  
  142.     if ( endTicks < TickCount() && part != kPlainArrow )
  143.     {
  144.         theValue = GetCtlValue( theControl );
  145.         theDialog = (DialogPtr)(*theControl )->contrlOwner;
  146.         GetDItem(theDialog, kTextItem, &iType, &iHandle, &iRect);
  147.         if ( part == inUpButton )        /* pressed up arrow, increment theValue */
  148.         {
  149.             if ( theValue < GetCtlMax( theControl ) )
  150.                 theValue++;
  151.             if ( oldDir != kUpArrow )    /* remember which part was pressed last */
  152.             {
  153.                 oldDir = kUpArrow;
  154.                 delay = kScrollDelay;
  155.             }
  156.         }
  157.         else if ( part == inDownButton )    /* pressed down arrow, decrement theValue */
  158.         {
  159.             if ( theValue > GetCtlMin( theControl ) )
  160.                 theValue--;
  161.             if ( oldDir != kDownArrow )    /* remember which part was pressed last */
  162.             {
  163.                 oldDir = kDownArrow;
  164.                 delay = kScrollDelay;
  165.             }
  166.         }
  167.         SetCtlValue( theControl, theValue );    /* Set the new control value */
  168.         NumToString( theValue, theString );        /* Convert the value to a string */
  169.         SetIText(iHandle, theString);            /* Set the text field to the new value */
  170.         SelIText(theDialog, kTextItem, 0, 32767);    /* hilite the text field */
  171.         
  172.         endTicks = TickCount() + delay;    /* determine when we will allow a value change */
  173.         if ( delay > 0 )    /* decrement the delay if we can */
  174.         {
  175.             delay--;
  176.         }
  177.     }
  178. }
  179.             
  180. /*******************************************************************************
  181.  * FUNCTION:    PositionDialog
  182.  *
  183.  * INPUT:        DialogPtr theDialog    -- the dialog to position on the screen.
  184.  *                Boolean move        -- should we actually move the dialog?
  185.  * RETURNS:        Point                -- new top left corner of the dialog
  186.  *
  187.  * DESCRIPTION:    Positions the given dialog centered horizontally on the screen
  188.  *                and in the upper third. The new location of the dialog is
  189.  *                returned. If move is true, then the dialog is moved to the new
  190.  *                position as well.
  191.  *******************************************************************************/
  192. static Point PositionDialog( DialogPtr theDialog, Boolean move )
  193. {
  194.     short    width;        /* width of theDialog */
  195.     short    height;        /* height of theDialog */
  196.     Point    newLoc;        /* new location of theDialog */
  197.  
  198.     width = theDialog->portRect.right - theDialog->portRect.left;
  199.     height = theDialog->portRect.bottom - theDialog->portRect.top;
  200.     newLoc.h = ( screenBits.bounds.right - width ) / 2;
  201.     newLoc.v = ( screenBits.bounds.bottom - GetMBarHeight() - height ) / 3
  202.                 + GetMBarHeight();
  203.  
  204.     if ( move )    /*    if we're supposed to move the dialog, do so    */
  205.     {
  206.         MoveWindow( theDialog, newLoc.h, newLoc.v, true );
  207.     }
  208. }
  209.  
  210. /*******************************************************************************
  211.  * FUNCTION:    ButtonOutline
  212.  *
  213.  * INPUT:        DialogPtr theDialog    -- the dialog to draw in
  214.  *                short itemID        -- item determining the button outline
  215.  * RETURNS:        Nothing.
  216.  *
  217.  * DESCRIPTION:    Draws an outline for a button in the user item identified by
  218.  *                itemID.
  219.  *******************************************************************************/
  220. static pascal void ButtonOutline( DialogPtr theDialog, short itemID )
  221. {
  222.     short         itemType;        /* The type of the item in GetDItem */
  223.     Handle        theItem;        /* handle to the item */
  224.     Rect        itemBox;        /* the box containing the item */
  225.     PenState    savedPenState;    /* old pen state resored on exit */
  226.     GrafPtr        origPort;        /* the current port saved. restored on exit */
  227.     
  228.     GetPort( &origPort );    /* save the old port */
  229.     SetPort( theDialog );
  230.  
  231.     GetDItem( theDialog, itemID, &itemType, &theItem, &itemBox );
  232.  
  233.         /* Draw the button outline */
  234.     GetPenState( &savedPenState );
  235.     PenNormal();
  236.     PenSize( 3, 3 );
  237.     FrameRoundRect( &itemBox, 16, 16 );
  238.  
  239.         /* Restore the old settings */
  240.     SetPenState( &savedPenState );
  241.     SetPort( origPort );
  242. }
  243.  
  244. void main( void )
  245. {
  246.     GrafPtr        savePort;    /* The old graphics port */
  247.     DialogPtr    theDialog;    /* The dialog to display */
  248.     short        itemHit;    /* Last item hit in the dialog */
  249.     short        itemType;    /* The type of item */
  250.     Handle        itemHandle;    /* handle to the item */
  251.     Rect        itemRect;    /* an item's Rect */
  252.     Str255        theString;    /* misc. pascal string */
  253.     long        theValue;    /* misc value. */
  254.     Boolean        done;        /* is the program done yet? */
  255.     
  256.     ToolBoxInit();
  257.  
  258.     GetPort( &savePort );
  259.     
  260.     theDialog = GetNewDialog( kBaseResID, nil, kMoveToFront );
  261.     if ( theDialog == NULL )
  262.     {
  263.         SysBeep( 10 );
  264.         ExitToShell();
  265.     }
  266.  
  267.         /*    Center the dialog, select it, and make it
  268.         **    the current graphics port.
  269.         **/
  270.     PositionDialog( theDialog, true );
  271.     SelectWindow( theDialog );
  272.     SetPort( theDialog );
  273.  
  274.         /* Give the arrow control an action procedure */
  275.     GetDItem( theDialog, kArrowCntl, &itemType, &itemHandle, &itemRect );
  276.     SetCtlAction( (ControlHandle) itemHandle, ArrowActionProc );
  277.     
  278.         /* Install a routine to draw the default button outline to a user item. */
  279.     GetDItem( theDialog, kOKOutline, &itemType, &itemHandle, &itemRect );
  280.     SetDItem( theDialog, kOKOutline, itemType, &ButtonOutline, &itemRect );
  281.  
  282.         /* Make the arrow activator initially active. */
  283.     GetDItem( theDialog, kActiveItem, &itemType, &itemHandle, &itemRect );
  284.     SetCtlValue( (ControlHandle) itemHandle, kChecked );
  285.     
  286.         /* Initially use the color arrow */
  287.     GetDItem( theDialog, kUseColorArrow, &itemType, &itemHandle, &itemRect );
  288.     SetCtlValue( (ControlHandle) itemHandle, kChecked );
  289.     
  290.         /*    Done with initializations.
  291.         **    Show the dialog and call ModalDialog until OK is pressed.
  292.         **/
  293.     ShowWindow( theDialog );
  294.     done = false;
  295.     while( !done )
  296.     {
  297.         ModalDialog( DialogFilter, &itemHit );
  298.         switch ( itemHit )
  299.         {
  300.             case ok:            /* OK button pressed. Exit the program. */
  301.                 done = true;
  302.                 break;
  303.                 
  304.             case kTextItem:        /* Text entered, get the new value */
  305.                 GetDItem( theDialog, kTextItem, &itemType, &itemHandle, &itemRect );
  306.                 GetIText( itemHandle, theString );
  307.                 StringToNum( theString, &theValue );
  308.                 GetDItem( theDialog, kArrowCntl, &itemType, &itemHandle, &itemRect );
  309.                 SetCtlValue( (ControlHandle)itemHandle, theValue );
  310.                 break;
  311.                 
  312.             case kActiveItem:    /* Change the active status of the arrow control */
  313.                 GetDItem( theDialog, kActiveItem, &itemType, &itemHandle, &itemRect );
  314.                 theValue = 1 - GetCtlValue( (ControlHandle) itemHandle );
  315.                 SetCtlValue( (ControlHandle) itemHandle, theValue );
  316.                 GetDItem( theDialog, kArrowCntl, &itemType, &itemHandle, &itemRect );
  317.                 HiliteControl( (ControlHandle) itemHandle, (theValue) ? 0 : 255 );
  318.                 break;
  319.                 
  320.             case kUseColorArrow:
  321.                 GetDItem( theDialog, kUseColorArrow, &itemType, &itemHandle, &itemRect );
  322.                 theValue = 1 - GetCtlValue( (ControlHandle) itemHandle );
  323.                 SetCtlValue( (ControlHandle) itemHandle, theValue );
  324.                 GetDItem( theDialog, kArrowCntl, &itemType, &itemHandle, &itemRect );
  325.                 SetArrowAPIC( (ControlHandle) itemHandle,
  326.                               (theValue) ? kBWArrow : kColorArrow, true );
  327.                                                    
  328.             default:
  329.                 break;
  330.         }
  331.     }
  332.     
  333.     DisposDialog( theDialog );
  334.     SetPort( savePort );
  335.     ExitToShell();
  336. }